#include "Client.hpp"
#include <cstdio>
#include "Tournee.hpp"
#include "VRPTW.hpp"

#   define  LATENESS_SENSIVITY  20

Client::Client(	const VRPTW&    inVRPTW,
                unsigned 	inId,
				double 		inX,
				double		inY,
				double  	inDemand,
				double		inReadyTime,
				double		inDueDate,
				double		inServiceTime)
	: Vertex(inId, inX, inY), clVRPTW(inVRPTW), clLocked(false), clDemand(inDemand),
		clReadyTime(inReadyTime), clDueDate(inDueDate),
			clServiceTime(inServiceTime)
{
	//clTournee = 0;	
}

std::string		Client::toStr() const
{
	char		buffer[0x1000];
	
	sprintf(buffer, "%u (f%u) - %f - %f - %f - %f - %f - %f", 
				veId, veId + 2, veX, veY, clDemand, clReadyTime, clDueDate,
					clServiceTime);
	
	return (std::string(buffer));
}

/*
void			Client::changeTournee(Tournee* inTournee)
{
    clTournee->erase(veId);
    clTournee = inTournee;
}
*/


void			Client::test(TimeInfo*	inTimeInfo, double inArrivalDate, double inArrivalVehicleCapacity) EXC_THROWER()
{
    if (inTimeInfo == 0)
        EXC_THROW("timeinfo given is null");
    
    memset(inTimeInfo, 0, sizeof(TimeInfo));
    inTimeInfo->tiWaitForOpen = clReadyTime - inArrivalDate;
    
    if (inTimeInfo->tiWaitForOpen < 0.)
    {
        inTimeInfo->tiWaitForOpen = 0.;
        //inTimeInfo->tiArrivalAtLastDate = true;
    }
    inTimeInfo->tiStatus = TimeInfo::TI_OK;
    
    //std::cout << clDueDate - inArrivalDate << std::endl;
    
    // > ou bien >= ??
    if (inArrivalDate >= clDueDate)
        inTimeInfo->tiStatus = TimeInfo::TI_BAD_TIME;
    else if ((clDueDate - inArrivalDate) < clServiceTime)
    {
       // inTimeInfo->tiArrivalAtLastDate = true;
    }
    
    if ((inTimeInfo->tiTotalCapacity = (inArrivalVehicleCapacity + clDemand))
            > clVRPTW.getVehicleCapacity())
        inTimeInfo->tiStatus = TimeInfo::TI_BAD_CAPACITY;
    
    inTimeInfo->tiEndDate = inTimeInfo->tiWaitForOpen + clServiceTime + inArrivalDate;
    inTimeInfo->tiTotalTime = inTimeInfo->tiWaitForOpen + clServiceTime;
}
